//
//  EvaluateImagesCell.m
//  CenturyCity
//
//  Created by beisheng03 on 2018/1/8.
//  Copyright © 2018年 beisheng. All rights reserved.
//

#import "EvaluateImagesCell.h"
#import "PublishRenSubCell.h"
#import <ZLPhotoBrowser/ZLPhotoActionSheet.h>
@interface EvaluateImagesCell ()<UICollectionViewDelegate,UICollectionViewDataSource,PublishRenSubCellDelegate>
@property (weak, nonatomic) IBOutlet UILabel *topLabel;
@property (nonatomic, strong) NSMutableArray<PHAsset *> *lastSelectAssets;
@property (nonatomic, strong) NSMutableArray<UIImage *> *selectImages;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *labelX;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *labelY;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomMargin;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *collectViewH;

@property (weak, nonatomic) IBOutlet UICollectionView *myCollectionView;

@end

@implementation EvaluateImagesCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.bottomMargin.constant = 0;
    
    self.labelX.constant = ([UIScreen mainScreen].bounds.size.width - 51) / 4.0 + 20;
    
    self.labelY.constant = ([UIScreen mainScreen].bounds.size.width - 51) / 4.0 * 0.5;
    
    self.topLabel.textColor = placeholderColor;
    
    self.collectViewH.constant = ([UIScreen mainScreen].bounds.size.width - 51) / 4.0 + 20;
    
    self.topLabel.font = MYFONT(16);
    
    self.bottomMargin.constant = 0;
    
//    self.myCollectionView.backgroundColor = [UIColor purpleColor];
    
    
    [self.myCollectionView registerClass:[PublishRenSubCell class] forCellWithReuseIdentifier:[PublishRenSubCell identifierStr]];
    self.myCollectionView.delegate = self;
    self.myCollectionView.dataSource = self;
    // Initialization code
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    if (self.selectImages == nil) {
        
        return 1;
        
    }else{
        
        return self.selectImages.count + 1 > 8 ? 8 : self.selectImages.count + 1;
        
    }
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    PublishRenSubCell *cell = (PublishRenSubCell *)[collectionView dequeueReusableCellWithReuseIdentifier:[PublishRenSubCell identifierStr] forIndexPath:indexPath];
    
    cell.index = indexPath;
    
    
    cell.image = indexPath.item < self.selectImages.count ? self.selectImages[indexPath.item] : nil;
    
    cell.delegate = self;
    
    return cell;
    
}




- (void)photoCollectionCellAddBtnDidClick:(NSIndexPath *)index{
    
    
    
    
    
    ZLPhotoActionSheet *actionSheet = [[ZLPhotoActionSheet alloc] init];
    
    
    actionSheet.sender = self.viewController;
    
    actionSheet.navBarColor = themeColor;
    
    if (index.item != self.selectImages.count) {
        
        [actionSheet previewSelectedPhotos:self.selectImages assets:self.lastSelectAssets index:index.item isOriginal:YES];
        
        return;
    }
    
    
    actionSheet.sortAscending = YES;
    actionSheet.allowSelectImage = YES;
    actionSheet.allowSelectGif = NO;
    actionSheet.allowSelectVideo = NO;
    actionSheet.allowSelectLivePhoto = NO;
    actionSheet.allowForceTouch = NO;
    actionSheet.allowEditImage = YES;
    actionSheet.allowTakePhotoInLibrary = YES;
    actionSheet.showSelectedMask = YES;
    
    //设置照片最大选择数
    actionSheet.maxSelectCount = 4;
    
    actionSheet.arrSelectedAssets = self.lastSelectAssets;
    
    
    MJWeakSelf;
    [actionSheet setSelectImageBlock:^(NSArray<UIImage *> * _Nonnull images, NSArray<PHAsset *> * _Nonnull assets, BOOL isOriginal) {
        
        weakSelf.lastSelectAssets = [NSMutableArray arrayWithArray:assets];
        
        weakSelf.selectImages = [NSMutableArray arrayWithArray:images];
        
        weakSelf.model.imags = self.selectImages;
        
        if (weakSelf.selectImages.count > 0) {
            weakSelf.topLabel.hidden = YES;
        } else {
            weakSelf.topLabel.hidden = NO;
            
        }
        
//        if (weakSelf.selectImages.count + 1 < 5) {
//
//            weakSelf.myCollectionView.frame = CGRectMake(0, 0, KScreenW, (KScreenW - 40) / 3.0 + 20);
//
//        } else if (weakSelf.selectImages.count + 1 < 7){
//
//            self.myCollectionView.frame = CGRectMake(0, 0, KScreenW, ((KScreenW - 40) / 3.0 + 20)  * 2);
//
//        }else{
//
//            weakSelf.myCollectionView.frame = CGRectMake(0, 0, KScreenW, ((KScreenW - 40) / 3.0 + 20)  * 3);
//
//
//
//        }
        
        [weakSelf.myCollectionView reloadData];
        //your codes
    }];
    
    //调用相册
    [actionSheet showPhotoLibrary];
    
    
}

- (void)photoCollectionDeleBtnDidClick:(NSIndexPath *)index{
    
    
    
    
    [self.selectImages removeObjectAtIndex:index.item];
    
    [self.lastSelectAssets removeObjectAtIndex:index.item];
    
    if (self.selectImages.count > 0) {
        self.topLabel.hidden = YES;
    } else {
        self.topLabel.hidden = NO;
        
    }
    
    [self.myCollectionView reloadData];
    
    
    
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
